341D - Iahub and Xors - CodeForces Solution


data structures *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mxn 1010
ll multree[mxn][mxn][2], addtree[mxn][mxn][2];

ll yo(ll x) { return (x % 2); }

ll query2(ll tree[mxn][mxn][2], ll x, ll y) {
  ll mul = 0, add = 0;
  for (ll i = y; i > 0; i -= i & -i) {
    mul ^= tree[x][i][0];
    add ^= tree[x][i][1];
  }
  return (mul * yo(y)) ^ add;
}

ll query1(ll x, ll y) {
  ll mul = 0, add = 0;
  for (ll i = x; i > 0; i -= i & -i) {
    mul ^= query2(multree, i, y);
    add ^= query2(addtree, i, y);
  }
  return (mul * yo(x)) ^ add;
}

ll query(ll x1, ll y1, ll x2, ll y2) {
  return (query1(x2, y2) ^ query1(x1 - 1, y2) ^ query1(x2, y1 - 1) ^
          query1(x1 - 1, y1 - 1));
}

void upd2(ll tree[mxn][mxn][2], ll x, ll y, ll mul, ll add) {
  for (ll i = x; i < mxn; i += i & -i) {
    for (ll j = y; j < mxn; j += j & -j) {
      tree[i][j][0] ^= mul;
      tree[i][j][1] ^= add;
    }
  }
}

void upd1(ll x, ll y1, ll y2, ll mul, ll add) {
  upd2(multree, x, y1, mul, mul * yo(y1 - 1));
  upd2(multree, x, y2, mul, mul * yo(y2));
  upd2(addtree, x, y1, add, add * yo(y1 - 1));
  upd2(addtree, x, y2, add, add * yo(y2));
}

void upd(ll x1, ll y1, ll x2, ll y2, ll val) {
  upd1(x1, y1, y2, val, val * yo(x1 - 1));
  upd1(x2, y1, y2, val, val * yo(x2));
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  ll i, j, k, n, m, tt, x1, y1, x2, y2, q, val;
  cin >> n;
  cin >> q;
  while (q--) {
    cin >> tt;
    if (tt == 2) {
      cin >> x1 >> y1 >> x2 >> y2 >> val;
      upd(x1, y1, x2, y2, val);
    } else {
      cin >> x1 >> y1 >> x2 >> y2;
      cout << query(x1, y1, x2, y2) << "\n";
    }
  }
  return 0;
}
// ofwKQlZUxNLTgCtMklnkDeTJaaEEvyjUUvCqdRuwNOntcquIBeZYJAhOojCOEllbRiSNCHdrBjufoOMChMNZvYXpZGXBQLToRXycLhqrbyCvOORMyHVVQWlvadWkcZlT


Comments

Submit
0 Comments
More Questions

1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo
208D - Prizes Prizes more Prizes
659A - Round House
1492C - Maximum width
171B - Star
1512B - Almost Rectangle
831B - Keyboard Layouts
814A - An abandoned sentiment from past
268C - Beautiful Sets of Points
1391C - Cyclic Permutations
11A - Increasing Sequence
1406A - Subset Mex
1365F - Swaps Again
50B - Choosing Symbol Pairs
1719A - Chip Game
454B - Little Pony and Sort by Shift
1152A - Neko Finds Grapes
1719B - Mathematical Circus
1719C - Fighting Tournament
1642A - Hard Way
285C - Building Permutation
1719E - Fibonacci Strings
1696C - Fishingprince Plays With Array